<script setup lang="ts">
import { ref, onMounted } from 'vue'
import ClassCard from './ClassCard.vue'
import { getCurrentDayScheduleList } from '@/services/home.ts'
import type { ScheduleItem, ScheduleParams } from '@/types/schedule'
import { useMemberStore } from '@/stores'
//当前登录用户
const memberStore = useMemberStore()
const scheduleList = ref<ScheduleItem[]>([])

// 获取课程列表
const getCurrentDayScheduleListData = async () => {
  try {
    const res = await getCurrentDayScheduleList()
    // 添加数据
    scheduleList.value = res.data
  } catch (error) {
    console.error('获取课程列表失败:', error)
  }
}

// 状态管理
const isLoading = ref(false)
const isTriggered = ref(false)
const serviceItems = ref([
  {
    icon: 'https://167nj97th156.vicp.fun/profile/upload/student.png',
    name: '学生管理',
    color: '#409EFF',
    url: '/pages/student/index',
  },
  {
    icon: 'https://167nj97th156.vicp.fun/profile/upload/calendar.png',
    name: '课表',
    color: '#409EFF',
    url: '/pages/mySchedule/index',
  },
  {
    icon: 'https://167nj97th156.vicp.fun/profile/upload/homework.png',
    name: '课后作业',
    color: '#FF7D00',
    url: '/pages/homework/index',
  },
  {
    icon: 'https://167nj97th156.vicp.fun/profile/upload/chengzhangdangan.png',
    name: '成长档案',
    color: '#909399',
    url: '/pages/growthArchive/index',
  },
  {
    icon: 'https://167nj97th156.vicp.fun/profile/upload/ketangdianping.png',
    name: '课堂点评',
    color: '#909399',
    url: '/pages/growthArchive/index',
  },
  {
    icon: 'https://167nj97th156.vicp.fun/profile/upload/notice.png',
    name: '通知管理',
    color: '#909399',
    url: '/pages/notice/index',
  },
  {
    icon: 'https://167nj97th156.vicp.fun/profile/upload/jifen.png',
    name: '积分币管理',
    color: '#FF7D00',
    url: '/pages/points/studentPointsManager',
  },
  {
    icon: 'https://167nj97th156.vicp.fun/profile/upload/jifenlipin.png',
    name: '兑换管理',
    color: '#FF7D00',
    url: '/pages/points/exchangeRecord',
  },
])

// 生命周期
onMounted(async () => {
  isLoading.value = true
  await Promise.all([getCurrentDayScheduleListData()])
  isLoading.value = false
})

// 下拉刷新处理
const onRefresherrefresh = async () => {
  isTriggered.value = true
  await Promise.all([getCurrentDayScheduleListData()])
  // 关闭动画
  isTriggered.value = false
}

const navigateToSchedule = () => {
  uni.navigateTo({ url: '/pages/mySchedule/index' })
}
</script>

<template>
  <scroll-view
    class="page-container"
    :scroll-y="true"
    :refresher-enabled="true"
    @refresherrefresh="onRefresherrefresh"
    :refresher-triggered="isTriggered"
  >
    <!-- 服务图标区域 -->
    <view class="service-grid card-common">
      <navigator
        class="service-item"
        hover-class="none"
        :url="item.url"
        v-for="(item, index) in serviceItems"
        :key="index"
      >
        <view class="icon-container" :style="{ backgroundColor: item.color + '15' }"
          ><!-- 使用颜色值加透明度作为背景 -->
          <image class="icon" :src="item.icon"></image>
        </view>
        <text class="service-name">{{ item.name }}</text>
      </navigator>
    </view>

    <!-- 今日课表 -->
    <view class="section-container card-common">
      <view class="section-header flex-row justify-between items-center">
        <text class="section-title">今日课表</text>
        <text class="more-link" @click="navigateToSchedule">{{ '更多>' }}</text>
      </view>
      <view class="schedule-content">
        <ClassCard v-for="course in scheduleList" :key="course.id" :schedule="course" />
        <view class="empty-state" v-if="scheduleList.length === 0 && !isLoading">
          <image class="empty-icon" src="@/static/images/empty.png"></image>
          <text class="empty-text">暂无课程</text>
        </view>
      </view>
    </view>
  </scroll-view>
</template>

<style lang="scss">
@import '../styles/index.scss';
</style>
